module.exports = (request, response, next) => {
    if (request.method === 'POST') {
        request.method = 'GET'
    }


    if (request.method === 'DELETE') {
        let db_table_name_arr = request._parsedUrl.pathname.split('/')
        let db_table_name = db_table_name_arr[db_table_name_arr.length - 1]
        request.method = 'GET'
        const db_file_name = db_table_name + '.js'
        try {
            const { deleteFn } = require('./db/' + db_file_name)
            deleteFn(request.query, (err) => {
                if (err) {
                    response.send = () => {
                        response.end(JSON.stringify({
                            status: 401,
                            message: err
                        }))
                    }
                } else {
                    response.send = () => {
                        response.end(JSON.stringify({
                            status: 200,
                            message: 'delete success'
                        }))
                    }
                }
            })
            next()
        } catch (err) {
            response.send = () => {
                response.end(JSON.stringify({
                    status: 401,
                    ...err
                }))
            }
            next()
        }

    }

    // // 处理ie8下的文件上传
    // if ((request.headers['content-type'] || '').startsWith('multipart/form-data')) {
    //     response.header('content-type', 'text/html')
    // }

    response.send = (data) => {
        response.end(JSON.stringify({
            status: 200,
            data: JSON.parse(data),
            message: 'success'
        }))
    }
    next()
}

